#include "rawimage.h"
#include "ui_rawimage.h"
#include "histwidget.h"
#include "imageprocessor.h"
RawImage::RawImage(QImage *image,float* hist,QWidget *parent):
    QDialog(parent),
    ui(new Ui::RawImage)
{
    memcpy(this->hist,hist,sizeof(float)*256);
    ui->setupUi(this);
    this->ui->widget->SetHistogram(hist);
    this->preview_image = PointImageOperation(
                &image->scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio),
                GrayOperation);
    this->ui->label->setPixmap(QPixmap::fromImage(preview_image));
    this->bin = -1;
    isEq= false;
}

RawImage::~RawImage()
{
    delete ui;
}





void RawImage::on_Manul_toggled(bool checked)
{
    this->ui->horizontalSlider->setEnabled(checked);
}

void RawImage::on_ok_button_clicked()
{
    bin = this->ui->horizontalSlider->value();
    this->accept();
}

void RawImage::on_OSTU_toggled(bool checked)
{
    if(checked)
    {
        this->ui->horizontalSlider->setValue(GetOstuThreshold(hist));
    }
}

void RawImage::on_radioButton_3_toggled(bool checked)
{
    if(checked)
    {
        this->ui->horizontalSlider->setValue(GetThresholdEntropy(hist));
    }
}

void RawImage::on_Cancel_clicked()
{
    this->reject();
}

void RawImage::on_horizontalSlider_valueChanged(int value)
{
    this->ui->widget->SetThreshold(value);
    if(value>=0)
        this->ui->label->setPixmap(QPixmap::fromImage(
                                   PointImageOperation(&preview_image,BinOperation,&value)));
    else
        this->ui->label->setPixmap(QPixmap::fromImage(preview_image));
}

void RawImage::on_pushButton_clicked()
{
    float temp[256];
    Equalization(this->hist,temp,map);
    memcpy(hist,temp,sizeof(float)*256);
    ui->widget->SetHistogram(hist);
    this->preview_image = PointImageOperation(&preview_image,RemapOperation,map);
    this->ui->label->setPixmap(QPixmap::fromImage(preview_image));
    isEq = true;
}
